<?php

class controller_group extends __registry_controller
{
    public $_ = array();
    public $_layout = NULL;

    public $_content_top = NULL;
    public $_content_bot = NULL;
    public $_content_right = NULL;
    public $_content_left = NULL;
    public $_content_header = 'OPEN';
    public $_content_footer = NULL;
    public $_table = 'groups';
    public $_table_second = 'group_permiss';

    public function __construct()
    {	
		$this->_layout =NULL;
        parent::add_object(array(
            'model_group',
            'handle_paging',
            'system_model',
            'model_permission'
        ));
	}
	
	public function index_action()
	{
	   /* Check permission */
       $this->per_check_permission();
       
       $seo2 = isset($this->_seokey[2])?$this->_seokey[2]:null;
       $this->_['com'] = $this->_seokey[1];
       
       /*****************pagination**********************/
       $limit = 5;
       $current = end($this->_seokey)?intval(end($this->_seokey)):1;
       $total = $this->mdg_get_total($this->_table);
       $url = !empty($cid)?URL_BASE.$this->_seokey[0].'/'.$this->_seokey[1].'/'.$seo2:URL_BASE.$this->_seokey[0].'/'.$this->_seokey[1];
       $this->_['paging'] = $this->paging_display($total, $current, $limit, $url, $this->_,'/','.htm');
       
       $this->_['url_add'] = URL_BASE.'admin/'.$this->_seokey[1].'/'.$this->route->rt_get_seokey_act('add');
       $this->_['url_edit'] = URL_BASE.'admin/'.$this->_seokey[1].'/'.$this->route->rt_get_seokey_act('edit');
	   
       $strHtmlContent = '';
       $strOkHtml = '';
       $html = $this->view->view_display("index");
       $arrHtml = $this->view->view_get_block_html($html);
       
       $list_data = $this->mdg_get_list_data($this->_table,$current,$limit);
       if(!empty($list_data))
       {
            foreach($list_data as $list_item)
            {
                $strOkHtml .= $this->view->view_assign($arrHtml['list_item'],$list_item);
            }
       }
       $strHtmlContent = $this->view->view_assign_block($html, array('list'=>$strOkHtml));
	   return $strHtmlContent;
	}

    public function add_action(){
        /* Check permission */
        $this->per_check_permission();
       
        $this->_['label-error'] = '';
        $this->_layout = 'iframe';
        $this->_['com'] = 'permission';
        $this->_['error'] = '';
        $strHtmlContent = '';
        $strPermission = '';
        $list_permission = $this->mdp_get_list_permission(1,1000,null,'CID');
        
        if(isset($_POST) && !empty($_POST))
        {
            if(!empty($_POST['group_name']))
            {
                $group_name = $_POST['group_name'];
                $data['GroupName'] = $_POST['group_name'];
                if($_POST['Status'] == 'show')
				{
                    $data['Status'] = 'true';
                }else{
                    $data['Status'] = 'false';
                }
                $this->mdg_insert_data($this->_table,$data);
                $last_id_group = $this->mdg_get_last_id($this->_table);
                if(isset($_POST['permission']) && !empty($_POST['permission']))
                {
                    $data_group_permission = array();
                    foreach($_POST['permission'] as $value){
                        $data_group_permission['PremissID'] =$value;
                        $data_group_permission['GroupID'] =$last_id_group;
                        $this->mdg_insert_data($this->_table_second, $data_group_permission);
                    } 
                }
                $this->request->rq_goto_window_parent(URL_BASE.$this->_seokey[0].'/'.$this->_seokey['1']); 
            }else{
                $this->_['error'] = $this->_['error_group_name'];
            }
        }
        
        $strHtml = $this->view->view_display('add');
        $arrHtml = $this->view->view_get_block_html($strHtml);
        $strPermissionGroup = '';
        $cid = 0;
        if(!empty($list_permission))
        {
            foreach($list_permission as $key=>$value)
            {
                if($key == 0)
                {
                    $cid = $value['CID'];
                    $group_name = ucwords($value['CatName']);
                }
                if($cid != $value['CID'])
                {
                    $strPermissionGroup .= $this->view->view_assign($arrHtml['group_permission'],
                                                            array(
                                                                    'permision_list'   =>  $strPermission,
                                                                    'group_name'=>$group_name
                                                                )
                    );
                    $strPermission = '';
                    $cid = $value['CID'];
                    $group_name = ucwords($value['CatName']);                    
                }
                $strPermission .= $this->view->view_assign($arrHtml['permission'],
                                                            array(
                                                                    'permission_name'   =>  $value['PremissName'],
                                                                    'permission_id'           => $value['ID']
                                                            )
                );
                
                if(count($list_permission)-1 == $key)
                {
                    $strPermissionGroup .= $this->view->view_assign($arrHtml['group_permission'],
                                                            array(
                                                                    'permision_list'   =>  $strPermission,
                                                                    'group_name'=>$group_name
                                                                )
                    );
                }
                
            }
        }
        
        $strHtmlContent = $this->view->view_assign_block($strHtml, 
                                                         array(
                                                            'list_permission'=>'',
                                                            'group_permission_list'=>$strPermissionGroup
                                                            )
        );
        
        return $strHtmlContent;
    }
    
    public function edit_action(){
        /* Check permission */
        $this->per_check_permission();

        $this->_['label-error'] = '';
        $this->_layout = 'iframe';
        $this->_['com'] = 'permission';
        $this->_['error'] = '';
        $id = $this->_seokey[3];
        $strHtmlContent = '';
        $strPermission = '';
        $list_permission = $this->mdp_get_list_permission(1,1000,null,'CID');
        $get_group = $this->mdg_get_group($id);
        /******update data**************/
        if(isset($_POST) && !empty($_POST))
        {
            if(!empty($_POST['group_name']))
            {
                $group_name = $_POST['group_name'];
                $data['GroupName'] = $_POST['group_name'];
                if($_POST['Status'] == 'show')
                {
                    $data['Status'] = 'true';
                }else
                {
                    $data['Status'] = 'false';
                }
                $this->mdg_update_data($this->_table,$data, 'id = '.$id);
                $this->mdg_del_item($this->_table_second, 'GroupID', $id);
                if(isset($_POST['permission']) && !empty($_POST['permission']))
                {
                    $data_group_permission = array();
                    foreach($_POST['permission'] as $value)
                    {
                        $data_group_permission['PremissID'] =$value;
                        $data_group_permission['GroupID'] =$id;
                        $this->mdg_insert_data($this->_table_second, $data_group_permission);
                    } 
                }
                $this->request->rq_goto_window_parent(URL_BASE.$this->_seokey[0].'/'.$this->_seokey['1']); 
            }else{
                $this->_['label-error'] = $this->_['error_group_name'];
            }
        }
        /***************show data********************/
        $strHtml = $this->view->view_display('edit');
        $arrHtml = $this->view->view_get_block_html($strHtml);
        
        $this->_['group_value'] = $get_group['GroupName'];
        if($get_group['Status'] == 'false')
        {
            $this->_['select_status'] = 'selected=""';
        }else{
            $this->_['select_status'] = '';
        }
        
        $strPermissionGroup = '';        
        $cid = 0;                
        if(!empty($list_permission))
        {                                    
            $list_group_permission = $this->mdg_get_group_permission($id);
            $array_list_id_permission = array();
            if(!empty($list_group_permission))
            {
                foreach($list_group_permission as $value)
                {
                    $array_list_id_permission[] = $value['PremissID'];
                }
            }
            foreach($list_permission as $key=>$value)
            {
                if($key == 0)
                {
                    $cid = $value['CID'];
                    $group_name = ucwords($value['CatName']);
                }
                if($cid != $value['CID'])
                {
                    $strPermissionGroup .= $this->view->view_assign($arrHtml['group_permission'],
                                                            array(
                                                                    'permision_list'   =>  $strPermission,
                                                                    'group_name'=>$group_name
                                                                )
                    );
                    $strPermission = '';
                    $cid = $value['CID'];
                    $group_name = ucwords($value['CatName']);
                }
                                
                if(in_array($value['ID'], $array_list_id_permission))
                {
                    $checked = 'checked=""';
                }else{
                    $checked = '';
                }
                $strPermission .= $this->view->view_assign($arrHtml['permission'],
                                                            array(
                                                                    'permission_name'       =>  $value['PremissName'],
                                                                    'permission_id'         => $value['ID'],
                                                                    'checked'               => $checked
                                                            )
                );
                
                if(count($list_permission)-1 == $key)
                {
                    $strPermissionGroup .= $this->view->view_assign($arrHtml['group_permission'],
                                                            array(
                                                                    'permision_list'   =>  $strPermission,
                                                                    'group_name'=>$group_name
                                                                )
                    );
                }
            }
        }
        $strHtmlContent = $this->view->view_assign_block($strHtml, array(
                                                                    'list_permission'=>'',
                                                                    'group_permission_list'=>$strPermissionGroup
                                                                    ));
        
        return $strHtmlContent;
    }
    
    public function index_ajax_action(){
        /* Check permission */
        $this->per_check_permission();

        $this->_layout = 'NO_LAYOUT';
        $html = '';
        $action = isset($_POST['action'])?$_POST['action']:'';
        
        if(isset($_POST['action']))
        {
            switch($action)
            {
               case "delete":
                   $html = $this->deleteItem();
                   break;
               default:
                   break;
            }
            return $html;    
        }else
        {
            $active = isset($this->_seokey[3])?$this->_seokey[3]:null;
            $str_id = isset($this->_seokey[4])?$this->_seokey[4]:null;
            $str_path = isset($this->_seokey[5])?$this->_seokey[5]:null;
            $done = $this->mdg_tool_func('groups',$this->model,$active,$str_id);
            if($done){
                return 'ok';
            }else{
                return 'noOk';
            } 
        }
        
    }
    
    public function getAction()
    {
        if(isset($_POST['id_seokey']) && !empty($_POST['id_seokey']))
        {
            $id_seokey = (int)$_POST['id_seokey'];
            $get_action_from_seokey = $this->mdp_get_action_from_seokey($id_seokey);
            if(!empty($get_action_from_seokey))
            {
                $html = '<select name="action" class="field select small" tabindex="3">';
                foreach($get_action_from_seokey as $value)
                {
                    $html .= '<option value="'.$value['ActName'].'">'.$value['ActName'].'</option>';
                }
                $html .= '</select>';
            }else{
                $html = 'no-data';
            }
        }else{
            $html =  'no-data';
        }
        
        return $html;
    }
    
    
}